package cn.bigcontainer.alt.sys.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.bigcontainer.alt.meta.mapper.MAppMapper;
import cn.bigcontainer.alt.meta.po.MApp;
import cn.bigcontainer.alt.sys.mapper.MenuMapper;
import cn.bigcontainer.alt.sys.po.Menu;
import cn.bigcontainer.alt.sys.service.SysMenuService;

/**
 * 菜单的Service接口实现类.
 *
 * @author jiaod
 * @date 2020-04-13
 */
@Service
@Transactional
public class SysMenuServiceImpl implements SysMenuService {

	@Resource
	private MenuMapper menuMapper;

	@Resource
	private MAppMapper appMapper;
	
	private void setAppMenuNo (String oldNo, String newNo) {
		List<MApp> apps = appMapper.selectList(new QueryWrapper<MApp>().like("data", "%\"menu\":\"" + oldNo + "\"%"));
		for (MApp app : apps) {
			app.getData().bigPut("menu", newNo);
			appMapper.updateById(app);
		}
	}
	
	private void exchangeAppMenuNo (String no1, String no2) {
		List<MApp> apps = appMapper.selectList(new QueryWrapper<MApp>().like("data", "%\"menu\":\"" + no1 + "\"%"));
		List<MApp> apps2 = appMapper.selectList(new QueryWrapper<MApp>().like("data", "%\"menu\":\"" + no2 + "\"%"));
		for (MApp app : apps) {
			app.getData().bigPut("menu", no2);
			appMapper.updateById(app);
		}
		for (MApp app : apps2) {
			app.getData().bigPut("menu", no1);
			appMapper.updateById(app);
		}
	}
	
	private void exchageChildrenNo (String parentNo1, String parentNo2, List<Menu> list) {
		List<Menu> list1 = list.stream().filter(m -> !m.getNo().equals(parentNo1) && m.getNo().startsWith(parentNo1)).collect(Collectors.toList());
		List<Menu> list2 = list.stream().filter(m -> !m.getNo().equals(parentNo2) && m.getNo().startsWith(parentNo2)).collect(Collectors.toList());
		list1.forEach(m -> {
			m.setNo(parentNo2 + m.getNo().substring(parentNo1.length()));
			menuMapper.updateById(m);
		});
		list2.forEach(m -> {
			m.setNo(parentNo1 + m.getNo().substring(parentNo2.length()));
			menuMapper.updateById(m);
		});
	}
	
	@Override
	public void move(Integer selectedIds, Integer offset) {
		offset = offset > 0 ? 1 : -1;
		List<Menu> list = menuMapper.selectList(new QueryWrapper<Menu>().orderByAsc("no"));
		for (int i = 0; i < list.size(); i++) {
			Menu menu = list.get(i);
			if (!menu.getId().equals(selectedIds)) {
				continue;
			}
			Menu menu2 = null;
			if (offset > 0) {
				if (i == (list.size() - 1)) {
					continue;
				}
				// 往后找同级菜单的下一个
				for (int j = i + 1; j < list.size(); j ++) {
					if (list.get(j).getNo().length() == menu.getNo().length()) {
						menu2 = list.get(j);
						break;
					}
				}
			} else {
				if (i == 0) {
					continue;
				}
				// 往前找同级菜单的前一个
				for (int j = i - 1; j >= 0; j --) {
					if (list.get(j).getNo().length() == menu.getNo().length()) {
						menu2 = list.get(j);
						break;
					}
				}
			}
			if (menu2 == null) {
				break;
			}
			String no = menu.getNo();
			menu.setNo(menu2.getNo());
			menu2.setNo(no);
			exchageChildrenNo(menu2.getNo(), menu.getNo(), list);
			menuMapper.updateById(menu);
			menuMapper.updateById(menu2);
			break;
		}
	}
}
